import { Message } from 'element-ui'
import { toFixedCustomize } from '@/utils/bizUtils';
/**
 * 计算税费比率
 * @param {* 发票类型} invoiceType
 * @param {* 税率} rate
 * 税费比率计算公式：
 * 发票类型为无票时，税费比率=0.75
 * 发票类型为普通发票时，税费比率=0.87
 * 发票类型为增值税专用发票且税率为 0 时， 税费比率=0.87
 * 发票类型为增值税专用发票且税率为 0.01 或 0.03 时 税费比率=0.9
 */
const generateTaxRate = (invoiceType, rate) => {
  if (!invoiceType || !rate) {
    return false;
  }
  if (isNaN(parseFloat(invoiceType)) || isNaN(parseFloat(rate))) {
    return false;
  }
  // 101 普通发票，102 增值随发票，103 无税票
  if (invoiceType == 101 || (invoiceType == 102 && rate == 0)) { // 普通发票 或 增值税发票且税率为 0
    return 0.87;
  } else if (invoiceType == 103) { // 无税票
    return 0.75;
  } else if (invoiceType == 102 && (rate == 0.01 || rate == 0.03)) { // 增值税发票 税率为 0.01 或 0.03
    return 0.9;
  } else {
    return 1;
  }
}
/**
 * 新增进价计算单计量
 * @param {* Objecet} row 当前修改数
 */
export const handlePriceChangeForSingleAdd = (row) => {
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatio || 0) + parseFloat(row.packRatio || 0) + parseFloat(row.processChargesRatio || 0) - row.rebateRatio, 1000)
  if (taxRate) {
    if (row.addPriceType == '1') {
      row.mainUnitCostPrice = toFixedCustomize(row.mainPurchasePrice / taxRate + extraAmount);
    } else {
      row.mainUnitCostPrice = toFixedCustomize(row.mainPurchasePrice / taxRate * (1 + extraAmount));
    }
  } else {
    if (document.getElementsByClassName('el-message').length === 0) {
      Message.warning('发票类型、税率异常')
    }
    row.mainUnitCostPrice = 0;
  }
}
/* 获取价格 */
export const getPriceChangeForSingleAdd = row => {
  let mainUnitCostPrice
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatio || 0) + parseFloat(row.packRatio || 0) + parseFloat(row.processChargesRatio || 0) - row.rebateRatio, 1000)
  if (taxRate) {
    if (row.addPriceType == '1') {
      mainUnitCostPrice = toFixedCustomize(row.mainPurchasePrice / taxRate + extraAmount);
    } else {
      mainUnitCostPrice = toFixedCustomize(row.mainPurchasePrice / taxRate * (1 + extraAmount));
    }
  } else {
    mainUnitCostPrice = 0;
  }
  return mainUnitCostPrice
}

/**
 * 新增多供进价计算多计量
 * @param {* Objecet} row 当前修改数
 * @param {* Number} taxRate 税费比率
 */
export const handlePriceChangeForMultipleAdd = (row) => {
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatio || 0) + parseFloat(row.packRatio || 0) + parseFloat(row.processChargesRatio || 0) - row.rebateRatio, 1000)
  // 主计量进价 = 辅计量进价 * 实际转换率
  row.mainPurchasePrice = toFixedCustomize(row.purchasePrice * row.actualConvertRatio, 1000);
  if (taxRate) {
    if (row.addPriceType == '1') {
      // 实际转换率辅计量成本价 = 辅计量进价 + 其他费用
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate + extraAmount);
    } else {
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate * (1 + extraAmount));
    }
  } else {
    if (document.getElementsByClassName('el-message').length === 0) {
      Message.warning('发票类型、税率异常')
    }
    row.actualConvertRatioCostPrice = 0;
  }
  // 主计量成本价 = 实际转换率辅计量成本价 * 实际转换率
  row.mainUnitCostPrice = toFixedCustomize(row.actualConvertRatioCostPrice * row.actualConvertRatio);
  if (row.theoryConvertRatio && row.theoryConvertRatio != 0) {
    // 理论转换率辅计量成本价 = 主计量成本价 / 理论转换率
    row.theoryConvertRatioCostPrice = toFixedCustomize(row.mainUnitCostPrice / row.theoryConvertRatio);
  }
}
/* 获取价格 */
export const getPriceChangeForMultipleAdd = row => {
  let mainUnitCostPrice
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatio || 0) + parseFloat(row.packRatio || 0) + parseFloat(row.processChargesRatio || 0) - row.rebateRatio, 1000)
  // 主计量进价 = 辅计量进价 * 实际转换率
  row.mainPurchasePrice = toFixedCustomize(row.purchasePrice * row.actualConvertRatio, 1000);
  if (taxRate) {
    if (row.addPriceType == '1') {
      // 实际转换率辅计量成本价 = 辅计量进价 + 其他费用
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate + extraAmount);
    } else {
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate * (1 + extraAmount));
    }
  } else {
    row.actualConvertRatioCostPrice = 0;
  }
  // 主计量成本价 = 实际转换率辅计量成本价 * 实际转换率
  mainUnitCostPrice = toFixedCustomize(row.actualConvertRatioCostPrice * row.actualConvertRatio);
  return mainUnitCostPrice
}

/**
 * 新增进价计算多计量
 * @param {* Objecet} data 当前修改数，当前字段
 * @param {* Number} supplyPriceType 供货价格方式
 * @param {* Array} areaTableData 区域进价表格数据
 */
/* 字段注释：
    freightRatio - 运费比例或金额
    packRatio - 包装比例或金额
    processChargesRatio - 加工费比例或金额
    rebateRatio - 返利折合比例或金额
    theoryConvertRatioCostPrice - 理论转换率辅计量成本价
    actualConvertRatioCostPrice - 实际转换率辅计量成本价
    mainUnitCostPrice - 主计量成本价
    mainPurchasePrice - 主计进价
    purchasePrice - 辅计量进价
    actualConvertRatio - 实际转换率
  */
export const handleSpecialPriceChangeForMultipleAdd = (data, supplyPriceType, areaTableData) => {
  const {
    row,
    columnName = ''
  } = data;
  // 税费比率
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatio || 0) + parseFloat(row.packRatio || 0) + parseFloat(row.processChargesRatio || 0) - row.rebateRatio, 1000)
  // 主计量进价 = 辅计量进价 * 实际转换率
  row.mainPurchasePrice = toFixedCustomize(row.purchasePrice * row.actualConvertRatio, 1000);
  if (taxRate) {
    if (row.addPriceType == '1') {
      // 实际转换率辅计量成本价 = 辅计量进价 + 其他费用
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate + extraAmount);
    } else {
      row.actualConvertRatioCostPrice = toFixedCustomize(row.purchasePrice / taxRate * (1 + extraAmount));
    }
  } else {
    if (document.getElementsByClassName('el-message').length === 0) {
      Message.warning('发票类型、税率异常')
    }
    row.actualConvertRatioCostPrice = 0;
  }
  // 主计量成本价 = 实际转换率辅计量成本价 * 实际转换率
  row.mainUnitCostPrice = toFixedCustomize(row.actualConvertRatioCostPrice * row.actualConvertRatio);
  if (row.theoryConvertRatio && row.theoryConvertRatio != 0) {
    // 理论转换率辅计量成本价 = 主计量成本价 / 理论转换率
    row.theoryConvertRatioCostPrice = toFixedCustomize(row.mainUnitCostPrice / row.theoryConvertRatio);
  }
  if (supplyPriceType != 1 && (columnName == 'theoryConvertRatio' || columnName == 'actualConvertRatio')) {
    areaTableData.forEach(item => {
      if (item.skuNo == row.skuNo) {
        if (columnName == 'actualConvertRatio') {
          item.actualConvertRatio = row.actualConvertRatio || 0;
          // 主计量进价
          item.mainPurchasePrice = toFixedCustomize(item.purchasePrice * item.actualConvertRatio, 1000);
          // 主计量成本价
          item.mainUnitCostPrice = toFixedCustomize(item.actualConvertRatioCostPrice * item.actualConvertRatio);
        }
        item.theoryConvertRatio = row.theoryConvertRatio || 0;
        if (item.theoryConvertRatio && item.theoryConvertRatio != 0) {
          // 理论转换率辅计量成本价 = 主计量成本价 / 理论转换率
          item.theoryConvertRatioCostPrice = toFixedCustomize(
            item.mainUnitCostPrice / item.theoryConvertRatio
          );
        }
      }
    });
  }
}
/**
 * 修改进价计算单计量
 * @param {* Objecet} row 当前修改数
 * @param {* Number} taxRate 税费比率
 */
export const handlePriceChangeForSingleModify = (row) => {
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  // 运费、加工费、包装费、返利
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatioUpdate || 0) + parseFloat(row.packRatioUpdate || 0) + parseFloat(row.processChargesRatioUpdate || 0) - row.rebateRatioUpdate, 1000)
  // 成本价
  if (taxRate) {
    if (row.addPriceType == '1') {
      row.mainUnitCostPriceUpdate = toFixedCustomize(parseFloat(row.mainPurchasePriceUpdate || 0) / taxRate + extraAmount);
    } else {
      row.mainUnitCostPriceUpdate = toFixedCustomize(parseFloat(row.mainPurchasePriceUpdate || 0) / taxRate * (1 + extraAmount));
    }
  } else {
    if (document.getElementsByClassName('el-message').length === 0) {
      Message.warning('发票类型、税率异常')
    }
    row.mainUnitCostPriceUpdate = 0;
  }
  // 主计量进价浮动比率 = 主计量进价现值 - 原值 / 原值
  if (row.mainPurchasePrice) {
    row.mainPurchasePriceFloatRadio = toFixedCustomize((row.mainPurchasePriceUpdate - row.mainPurchasePrice) / row.mainPurchasePrice, 10000);
  } else {
    row.mainPurchasePriceFloatRadio = 0;
  }
  // 成本价浮动比率 = （主计量成本价现值-原值）/主计量成本价原值
  if (row.mainUnitCostPrice) {
    row.mainUnitCostFloatPrice = toFixedCustomize((row.mainUnitCostPriceUpdate - row.mainUnitCostPrice) / row.mainUnitCostPrice, 10000);
  } else {
    row.mainUnitCostFloatPrice = 0;
  }
}
/**
 * 修改进价计算多计量
 * @param {* Objecet} row 当前修改数
 * @param {* Number} taxRate 税费比率
 */
export const handlePriceChangeForMultipleModify = (row) => {
  let taxRate = generateTaxRate(row.invoiceType, row.taxRate)
  // 运费、加工费、包装费、返利
  let extraAmount = toFixedCustomize(parseFloat(row.freightRatioUpdate || 0) + parseFloat(row.packRatioUpdate || 0) + parseFloat(row.processChargesRatioUpdate || 0) - row.rebateRatioUpdate, 1000)
  // 主计量进价 = 辅计量进价*异常转换率
  row.mainPurchasePriceUpdate = toFixedCustomize(row.purchasePriceUpdate * row.abnormalPurchaseConvertRatioUpdate, 1000);
  // 主计量进价浮动比率 = 主计量进价现值 - 原值 / 原值
  if (row.mainPurchasePrice) {
    row.mainPurchasePriceFloatRadio = toFixedCustomize((row.mainPurchasePriceUpdate - row.mainPurchasePrice) / row.mainPurchasePrice, 10000);
  } else {
    row.mainPurchasePriceFloatRadio = 0;
  }
  // 计算实际转换率辅计量成本价, 主计量成本价
  if (taxRate) {
    if (row.addPriceType == '1') {
      // 实际转换率辅计量成本价 = 辅计量进价+（运费+包装+加工费-返利）
      row.actualConvertRatioCostPriceUpdate = toFixedCustomize(parseFloat(row.purchasePriceUpdate || 0) / taxRate + extraAmount);
    } else {
      // 实际转换率辅计量成本价 = 辅计量进价*（1+（运费+包装+加工费-返利）
      row.actualConvertRatioCostPriceUpdate = toFixedCustomize(parseFloat(row.purchasePriceUpdate || 0) / taxRate * (1 + extraAmount));
    }
  } else {
    if (document.getElementsByClassName('el-message').length === 0) {
      Message.warning('发票类型、税率异常')
    }
    row.actualConvertRatioCostPriceUpdate = 0;
  }
  // 主计量成本价 = 实际转换率辅计量成本价 * 异常转换率
  row.mainUnitCostPriceUpdate = toFixedCustomize(row.actualConvertRatioCostPriceUpdate * row.abnormalPurchaseConvertRatioUpdate);
  // 主计量成本价浮动比率 = （主计量成本价现值-原值）/主计量成本价原值
  if (row.mainUnitCostPrice) {
    row.mainUnitCostFloatPrice = toFixedCustomize((row.mainUnitCostPriceUpdate - row.mainUnitCostPrice) / row.mainUnitCostPrice, 10000);
  } else {
    row.mainUnitCostFloatPrice = 0;
  }
  // 理论转换率辅计量成本价 = 主计量成本价/理论转换率 theoryConvertRatio
  if (row.theoryConvertRatio) {
    row.theoryConvertRatioCostPriceUpdate = toFixedCustomize(row.mainUnitCostPriceUpdate / row.theoryConvertRatio);
  } else {
    row.theoryConvertRatioCostPriceUpdate = 0;
  }
}

/**
 * 判断存在价格为空的情况 - 单计量
 * @param {* Array} data 提交数据
 */
export const hasEmptyPriceForSingle = (data) => {
  let hasEmptyVal = data.some(
    item =>
      !item.mainUnitCostPrice ||
      item.mainPurchasePrice <= 0 ||
      item.mainUnitCostPrice <= 0
  );
  return { hasEmptyVal, msg: '进价、成本价不可以小于等于0' }
}
/**
 * 判断存在价格为空的情况 - 多计量
 * @param {* Array} data 提交数据
 */
export const hasEmptyPriceForMultiple = (data) => {
  let hasEmptyVal = data.some(
    item =>
      item.mainPurchasePrice <= 0 || // 主计量进价
      item.purchasePrice <= 0 || // 辅计量进价
      item.mainUnitCostPrice <= 0 || // 主计量成本
      item.theoryConvertRatioCostPrice <= 0 || // 理论转换率辅计量成本价
      !item.theoryConvertRatioCostPrice || // 理论转换率辅计量成本价
      item.actualConvertRatioCostPrice <= 0 || // 实际转换率辅计量成本价
      !item.actualConvertRatioCostPrice // 实际转换率辅计量成本价
  );
  return { hasEmptyVal, msg: '进价、成本价不可以小于等于0' }
}
/**
 * 格式化价格类型
 * @param {* string} val 1: 统一售价, 2: 区域售价
 */
export const formatSalePriceType = (val) => {
  if (!val) {
    return '';
  }
  if (val == 1) {
    return '统一售价';
  }
  if (val == 2) {
    return '区域售价';
  }
  return val;
}
/**
 * 格式化加价类型
 * @param {* string} val 1: 固定金额, 2: 比例
 */
export const formatAddPriceType = (val) => {
  if (!val) {
    return '';
  }
  if (val == 1) {
    return '固定金额';
  }
  if (val == 2) {
    return '比例';
  }
  return val;
}
